package CMobile
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	public class pm_result extends Object {
		private var _layer: Object;
		private var _bitmap: Bitmap;
		private var _rsize: Point;
		private var _size: Rectangle;
		private var _mpos: Point;
		private var _moff: Point;
		private var _source: Bitmap;
		private var _limit: Point;
		private var _scale: Number;
		public function pm_result(parent: Object, ax: Number, ay: Number, awidth: Number, aheight: Number) {
			super();
			_layer = new parent.app.classes['component'](parent, ax, ay, awidth, aheight, 'list');
			_bitmap = new Bitmap(new BitmapData(200, 200, false, 0));
			_layer.addChild(_bitmap);
			_layer.buttonMode = true;
			_layer.addEvent(MouseEvent.MOUSE_DOWN, doDown);
			_layer.addEvent(MouseEvent.MOUSE_UP, doUp);
			_layer.addEvent(MouseEvent.MOUSE_MOVE, doMove);
			_moff = new Point();
			reset(); 
		}

		protected function doDown(e: MouseEvent): void {
			_mpos = new Point(_layer.mouseX, _layer.mouseY);
		}
		
		protected function doUp(e: MouseEvent): void {
			_mpos = null;
		}
		
		public function get asParams(): String {
			return 'extends=mobil&m_offset=' + 
				(_limit.x * _rsize.x / _size.width) + ',' + (_limit.y * _rsize.y / _size.height) +
					'&m_size=' + _rsize.x + ',' + _rsize.y;
		}
		
		protected function doMove(e: MouseEvent): void {
			if (_mpos && _source) {
				reset(_source, _bitmap.width, _bitmap.height, _moff.add(new Point(_layer.mouseX - _mpos.x, _layer.mouseY - _mpos.y)));
				_mpos.x = _layer.mouseX;
				_mpos.y = _layer.mouseY;
			}
		}
		
		public function asBytes(): ByteArray {
			var bmd: BitmapData = new BitmapData(_rsize.x, _rsize.y, false);
			var matrix: Matrix = new Matrix();
			var p_scale	: Point = new Point(_rsize.x / _source.width, _rsize.y / _source.height);
			var r_scale: Number = p_scale.x > p_scale.y?p_scale.x:p_scale.y;
			
			matrix.scale(r_scale, r_scale);
			matrix.translate(_limit.x, _limit.y);
			bmd.draw(_source, matrix);
			var result: ByteArray = _layer.app.encodeFromJPG(bmd);
			bmd.dispose();
			return result;
		}
		
		public function reset(source: Bitmap=null, width: int=0, height: int=0, offset: Point = null): void {
			if (source) {
				_source = source;
				if (!width || (width > 1000)) width = 200;
				if (!height || (height > 1000)) height = 200;
				_rsize 	= new Point(width, height);
				_size 	= _layer.EnterHere(_rsize);
				if (_size.width < 1) _size.width = 1;
				if (_size.height < 1) _size.height = 1;
				
				var p_scale	: Point = new Point(_size.width / _source.width, _size.height / _source.height);
				_scale = p_scale.x > p_scale.y?p_scale.x:p_scale.y;
				_limit	= new Point((_size.width - _source.width * _scale) / 2,
									(_size.height - _source.height * _scale) / 2);
				if (offset) {
					_limit = _limit.add(offset);
					_moff = offset;
				} else {
/*					var mi: Rectangle = _layer.app.editor.cardBody.maskInfo.pos;
					var mr: Point = _layer.app.editor.cardBody.contentSize;
					var p: Point = new Point(mr.x - mi.right - mi.left, mr.y - mi.bottom - mi.top);
					var mo_scale: Number =  _scale * (_source.width / mr.x) * 0.5;
					p.x *= mo_scale;
					p.y *= mo_scale;
					var rb: Point = new Point(_limit.x + p.x + _source.width * _scale,
											_limit.y + p.y + _source.height * _scale);
					_moff = new Point(p.x, p.y);
					if (_limit.x + p.x > 0) _moff.x = 0;
					if (rb.x < _size.width) _moff.x = _size.width - _source.width * _scale;
					if (_limit.y + p.y > 0) _moff.y = 0;
					if (rb.y < _size.height) _moff.y = _size.height - _source.height * _scale;
						
					_limit = _limit.add(_moff);*/
					_moff = new Point();
				}
				
				if (_bitmap.bitmapData) _bitmap.bitmapData.dispose();
				_bitmap.bitmapData = new BitmapData(_size.width, _size.height, false, 0);
				_source.smoothing = true;
				
				var matrix: Matrix = new Matrix();
				matrix.scale(_scale, _scale);
				matrix.translate(_limit.x, _limit.y);
				_bitmap.bitmapData.draw(_source, matrix, null, null, null, true);
				
				_bitmap.x = _size.x;
				_bitmap.y = _size.y;
				_bitmap.width = _size.width;
				_bitmap.height = _size.height;
			} else {
				_bitmap.x = (_layer.width - _bitmap.width) / 2;
				_bitmap.y = (_layer.height - _bitmap.height) / 2;
			}
		}
	}
}